<template>
  <view v-if="cart.length">
    <!-- 收货地址区域 -->
    <my-address></my-address>
    <!-- 购物车列表区域 -->
    <view class="cart">
      <!-- 购物车商品列表的标题区域 -->
      <view class="cart-title">
        <!-- 左侧的图标 -->
        <uni-icons type="shop" size="18"></uni-icons>
        <!-- 描述文本 -->
        <text class="cart-title-text">购物车</text>
      </view>
      <!-- 购物车商品列表的商品区域 -->
      <view class="cart-list">
        <!-- 商品列表区域 -->
        <uni-swipe-action>
          <block v-for="(goods, i) in cart" :key="i">
            <uni-swipe-action-item :right-options="options" @click="swipeActionClickHandler(goods)">
              <!-- 复用my-goods组件 -->
              <goods-item @on-num-change="changeGoodsNumHandler" @on-radio-change="changeGoodsStateHandler" showRadio
                showNumberBox :goods="goods"></goods-item>
            </uni-swipe-action-item>
          </block>
        </uni-swipe-action>
      </view>
    </view>
    <!-- 结算 -->
    <my-settle></my-settle>
  </view>

  <!-- 空白购物车区域 -->
  <view class="empty-cart" v-else>
    <image src="/static/cart_empty@2x.png" class="empty-img"></image>
    <navigator  class="tip-text" open-type="switchTab" url="/pages/home/home">去逛逛</navigator>
  </view>
</template>

<script>
  import setBadge from '@/mixins/setBadge.js'
  import {
    mapState,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [setBadge],
    computed: {
      ...mapState('cart', ['cart'])
    },
    data() {
      return {
        // 滑块选项
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00'
          }
        }]
      }
    },
    methods: {
      ...mapMutations('cart', ['updateCart', 'deleteCart']),
      // 改变商品的选中状态
      changeGoodsStateHandler({
        id,
        checked_status
      }) {
        // 触发vuex中修改状态的方法
        const goods = this.cart.find(item => item.goods_id === id)
        if (goods) {
          goods.goods_state = checked_status
          // 修改vuex的数据
          this.updateCart(goods)
        }
      },
      // 改变商品数量
      changeGoodsNumHandler({
        id,
        new_goods_num
      }) {
        // 触发vuex中修改状态的方法
        const goods = this.cart.find(item => item.goods_id === id)
        if (goods) {
          goods.goods_count = new_goods_num
          // 修改vuex的数据
          this.updateCart(goods)
        }
      },
      // 删除购物车商品
      swipeActionClickHandler(goods) {
        this.deleteCart(goods)
      }
    }
  }
</script>

<style lang="scss">
  // 购物车商品列表
  .cart {

    // 购物车列表标题
    .cart-title {
      height: 40px;
      display: flex;
      align-items: center;
      font-size: 14px;
      padding-left: 5px;
      border-bottom: 1px solid #efefef;

      .cart-title-text {
        margin-left: 10px;
      }
    }
  }
  .empty-cart {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 150px;
  
    .empty-img {
      width: 90px;
      height: 90px;
    }
  
    .tip-text {
      font-size: 12px;
      color: gray;
      margin-top: 15px;
    }
  }
</style>
